/**
 * 
 */
package stc.skymobi.jcr;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.jcr.LoginException;
import javax.jcr.NoSuchWorkspaceException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;

import org.apache.jackrabbit.rmi.client.ClientRepositoryFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author hp
 *
 */
public class JcrMonitor {
	
    private static final Logger logger 
		= LoggerFactory.getLogger(JcrMonitor.class);
    
    private String  uri;
    private String  workspaceName;
    private String  user;
    private String  passwd;
    private String  pathName;
    private	boolean	isDeep;
    private	Session	session;
    private	List<EventListener>	eventListeners 
    	= new ArrayList<EventListener>();
    
    
    public boolean isDeep() {
		return isDeep;
	}

	public void setDeep(boolean isDeep) {
		this.isDeep = isDeep;
	}

	/**
     * @return the path
     */
    public String getPath() {
        return pathName;
    }

    /**
     * @param path the path to set
     */
    public void setPath(String path) {
        this.pathName = path.trim();
    }

    /**
     * @return the uri
     */
    public String getUri() {
        return uri;
    }

    /**
     * @param uri the uri to set
     */
    public void setUri(String uri) {
        this.uri = uri.trim();
    }

    /**
     * @return the workspaceName
     */
    public String getWorkspace() {
        return workspaceName;
    }

    /**
     * @param workspaceName the workspaceName to set
     */
    public void setWorkspace(String workspaceName) {
        this.workspaceName = workspaceName.trim();
    }

    /**
     * @return the user
     */
    public String getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(String user) {
        this.user = user.trim();
    }

    /**
     * @return the passwd
     */
    public String getPasswd() {
        return passwd;
    }

    /**
     * @param passwd the passwd to set
     */
    public void setPasswd(String passwd) {
        this.passwd = passwd.trim();
    }

	public void setEventListeners(List<EventListener> eventListeners) {
		this.eventListeners.addAll( eventListeners );
	}
	
    public void start() {
        Repository 	repository = null;
        ClientRepositoryFactory factory = new ClientRepositoryFactory();
        InputStream	ret = null;
        try {
            repository = factory.getRepository(uri);
            session = repository.login(
                    new SimpleCredentials(user, passwd.toCharArray()),
                    workspaceName);

            ObservationManager observationManager 
            	= session.getWorkspace().getObservationManager();

            for ( EventListener listener : eventListeners ) {
	            observationManager.addEventListener(listener, 
	            		Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED,
	            		pathName, isDeep, null, null, true);
            }
            
	    } catch (MalformedURLException e) {
	    	logger.error("start:", e);
	    } catch (NotBoundException e) {
	    	logger.error("start:", e);
	    } catch (RemoteException e) {
	    	logger.error("start:", e);
	    } catch (LoginException e) {
	    	logger.error("start:", e);
	    } catch (NoSuchWorkspaceException e) {
	    	logger.error("start:", e);
	    } catch (RepositoryException e) {
	    	logger.error("start:", e);
	    }
    }
    
    public void destroy() {
        if ( null != session ) {
            session.logout();
            session = null;
        }
    }

}
